using System;
using Autofac;
using Phoenix.Framework;

namespace Phoenix.Extensions.Autofac
{
    public class AutofacControllerFactory : ControllerFactoryBase
    {
        private IContainer container;

        public void SetContainer(IContainer autofacContainer)
        {
            container = autofacContainer;
        }

        protected override T CreateController<T>()
        {
            return container.Resolve<T>();
        }

        protected override Controller CreateController(Type controllerType)
        {
            return (Controller)container.Resolve(controllerType);
        }

        public override void Release(Controller controller)
        {
            var disposible = controller as IDisposable;
            if (disposible != null) disposible.Dispose();
        }
    }
}